package com.gurun.codes.business.login;

import android.content.Context;

import com.gurun.codes.R;
import com.gurun.codes.contract.base.BasePresenter;
import com.gurun.codes.contract.base.view.RxView;
import com.gurun.codes.utils.Constants;
import com.gurun.codes.utils.Md5;
import com.gurun.model.base.ResultObserver;
import com.gurun.model.base.RetrofitManager;
import com.gurun.model.bean.ApiResult;
import com.gurun.model.bean.user.LoginRequestBean;
import com.gurun.model.bean.user.LoginResultBean;
import com.gurun.model.service.UserService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface LoginActivityContract {

    interface LoginActivityView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(LoginResultBean datas);

        void onError(String msg);
    }

    class LoginActivityViewPresenter extends BasePresenter<LoginActivityView> {

        private Context mContext;

        public LoginActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        public void login(String account,String password) {
            LoginRequestBean loginRequst = new LoginRequestBean();
            loginRequst.setVersion(Constants.HTTP_VERSION);
            loginRequst.setLoginName(account);
            loginRequst.setPassword(Md5.md5(password));
            loginRequst.setUserSource(1);
            loginRequst.setPlatform(Constants.HTTP_PLATFORM);
            loginRequst.setChannel("");
            loginRequst.setLatitude("1");
            loginRequst.setLongitude("1");

            RetrofitManager.getInstance().createApiService(UserService.class)
                    .getTestDatas(loginRequst)
                    .compose(getView().<LoginResultBean>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new ResultObserver<LoginResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_connect_server_failed));
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }

                        @Override
                        public void onSuccess(LoginResultBean result, String msg) {
                            getView().onLoaded(result);
                        }

                        @Override
                        public void onApiError(ApiResult result) {
                            if (result != null) {
                                getView().onError(result.getRetMsg());
                            } else {
                                getView().onError(mContext.getString(R.string.common_connect_server_failed));
                            }
                        }
                    }));
        }

    }
}
